package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

import com.smalls.redshoes.ui.activity.LivePlayActivity;

public class ScrollTextView extends TextView {
    Handler mHandler;
    Runnable mRunnable;

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        startScroll();
    }

    private void startScroll() {
        initSrcoll();
        scrollTo(0, 0);
        this.mHandler.removeCallbacks(this.mRunnable);
        this.mHandler.postDelayed(this.mRunnable, 7000);
    }

    private void initSrcoll() {
        if (this.mHandler == null) {
            this.mHandler = new Handler();
        }
        if (this.mRunnable == null) {
            this.mRunnable = new Runnable() {
                public void run() {
                    int textAreaheight = (ScrollTextView.this.getHeight() - ScrollTextView.this.getPaddingBottom()) - ScrollTextView.this.getPaddingTop();
                    int scrollY = ScrollTextView.this.getScrollY();
                    int lineHeight = ScrollTextView.this.getLineHeight();
                    int lineCount = ScrollTextView.this.getLineCount();
                    if (((double) lineCount) > Math.floor((double) (textAreaheight / lineHeight))) {
                        if (scrollY >= lineCount * lineHeight) {
                            ScrollTextView.this.scrollTo(0, 0);
                            ScrollTextView.this.mHandler.postDelayed(this, LivePlayActivity.DELAY_CANCEL_CHANNEL_SEQUENCE_POP);
                            return;
                        }
                        ScrollTextView.this.scrollBy(0, 1);
                        ScrollTextView.this.mHandler.postDelayed(this, 50);
                    }
                }
            };
        }
    }
}
